package com.zhangzhan.gulimallproduct.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangzhan.gulimallcommcon.util.MyCollUtil;
import com.zhangzhan.gulimallcommcon.util.PageUtils;
import com.zhangzhan.gulimallcommcon.util.Query;
import com.zhangzhan.gulimallproduct.dao.SkuInfoDao;
import com.zhangzhan.gulimallproduct.entity.SkuInfoEntity;
import com.zhangzhan.gulimallproduct.service.SkuInfoService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }


    @Override
    public PageUtils queryPageDetail(Map<String, Object> params) {
        String key = (String) params.get("key");
        Object catelogId = params.get("catelogId");
        Object brandId = params.get("brandId");
        Object min = params.get("min");
        Object max = params.get("max");

        LambdaQueryWrapper<SkuInfoEntity> wrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(key)) {
            wrapper.and(wrap -> {
                wrap.eq(SkuInfoEntity::getSkuId, key).or().like(SkuInfoEntity::getSkuName, key);
            });
        }

        wrapper.eq(!Objects.isNull(catelogId) && !Objects.equals(catelogId, "0"), SkuInfoEntity::getCatalogId, catelogId)
                .eq(!Objects.isNull(brandId) && !Objects.equals(brandId, "0"), SkuInfoEntity::getBrandId, brandId);

        boolean minb = !Objects.isNull(min) && !Objects.equals(min, "0");
        boolean maxb = !Objects.isNull(max) && !Objects.equals(max, "0");
        if (minb && maxb) {
            wrapper.between(SkuInfoEntity::getPrice, min, max);
        } else {
            wrapper.eq(minb, SkuInfoEntity::getPrice, min)
                    .eq(maxb, SkuInfoEntity::getPrice, max);
        }

        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                wrapper
        );


        return new PageUtils(page);

    }

    @Override
    public String getSkuName(Long skuId) {
        SkuInfoEntity skuInfoEntity = this.getById(skuId);
        return skuInfoEntity.getSkuName();
    }

    @Override
    public List<String> skuAttrs(Long skuId) {
        return this.getBaseMapper().skuAttrs(skuId);
    }

    @Override
    public BigDecimal getPrice(Long skuId) {
        SkuInfoEntity info = this.getById(skuId);
        return info.getPrice();
    }

    @Override
    public Map<Long,SkuInfoEntity> getInfoByIds(List<Long> skuIds) {
        List<SkuInfoEntity> skuInfoEntities = this.list(Wrappers.<SkuInfoEntity>lambdaQuery()
                .in(SkuInfoEntity::getSkuId, skuIds));
        if (MyCollUtil.isNotEmpty(skuInfoEntities)) {
            Map<Long,SkuInfoEntity> mapRes = new HashMap<>();
            for (SkuInfoEntity skuInfoEntity : skuInfoEntities) {
                Map<Long, SkuInfoEntity> map = Map.of(skuInfoEntity.getSkuId(), skuInfoEntity);
                mapRes.putAll(map);
            }
            return mapRes;
        }

        return null;
    }

}